package com.cqc.stateframelayout01;

import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.lwkandroid.stateframelayout.StateFrameLayout;

public class StateFrameLayoutActivity extends AppCompatActivity {

    private Context context = this;
    private GridView gridView;
    private StateFrameLayout stateFrameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state_frame_layout);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("StateFrameLayout");

        String[] array = getResources().getStringArray(R.array.texts);

        stateFrameLayout = (StateFrameLayout) findViewById(R.id.stateFrameLayout);
        stateFrameLayout.setOnEmptyRetryListener(new StateFrameLayout.OnEmptyRetryListener() {
            @Override
            public void onEmptyRetry() {
                Toast.makeText(context,"数据为空，重新请求数据",Toast.LENGTH_SHORT).show();
            }
        });
        stateFrameLayout.setOnNetErrorRetryListener(new StateFrameLayout.OnNetErrorRetryListener() {
            @Override
            public void onNetErrorRetry() {
                Toast.makeText(context,"网路错误，重新请求数据",Toast.LENGTH_SHORT).show();
            }
        });

        gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        stateFrameLayout.changeState(StateFrameLayout.LOADING);
                        break;
                    case 1:
                        stateFrameLayout.changeState(StateFrameLayout.EMPTY);
                        break;
                    case 2:
                        stateFrameLayout.changeState(StateFrameLayout.NET_ERROR);
                        break;
                    case 3:
                        stateFrameLayout.changeState(StateFrameLayout.SUCCESS);
                        break;
                }
            }
        });
    }
}
